package mshop.com.schild_kotlin.base

import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Toast
import kotlinx.android.synthetic.main.comm_title_layout.*
import mshop.com.schild_kotlin.R
import mshop.com.schild_kotlin.events.EventConstant
import mshop.com.schild_kotlin.skin.MySkinUtil
import org.greenrobot.eventbus.EventBus
import mshop.com.schild_kotlin.events.MessageEvent
import org.greenrobot.eventbus.ThreadMode
import org.greenrobot.eventbus.Subscribe





/**
 * Created by mayi on 2018/5/23.
 * @Autor CaiWF
 * @Email 401885064@qq.com
 * @TODO
 */
abstract class BaseActivity : AppCompatActivity(), SwipeRefreshLayout.OnRefreshListener {
    val ProgressBarTiem: Long = 1000
    var refreshLayout: SwipeRefreshLayout? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        MySkinUtil.onCreateInit(this)//skin init
        super.onCreate(savedInstanceState)
        if (intent != null && intent.extras != null) getExtrasData(intent.extras)
        setContentView(getLayoutResources())
        if (getNeedRefresh()) refreshLayout = findViewById(R.id.refreshLayout)
        initView()
        initRefreshLayout()

    }

    fun getExtrasData(bundle: Bundle) {}
    abstract fun getLayoutResources(): Int
    abstract fun initView()
    abstract fun getNeedRefresh(): Boolean


    private fun initRefreshLayout() {
        if (getNeedRefresh() && refreshLayout != null && !refreshLayout!!.isRefreshing) {
            //设置下拉刷新监听
            refreshLayout!!.setOnRefreshListener(this)
            refreshLayout!!.isRefreshing = true
            refreshLayout!!.postDelayed({
                onRefresh()
            }, ProgressBarTiem)
        } else {
            onRefresh()
        }
    }


    //Toast提示
    fun toastShow(message: String) {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show()
    }

    /**
     * 返回键--标题--图标
     */
    fun commTitleBar(title: String) {
        bar_title_tv.text = title
        bar_back_iv.setOnClickListener {
            finish()
        }
    }

    /**
     * 返回键--标题--图标
     */
    fun commTitleBar(title: String, backVisibity: Int, rightVisibity: Int, rightImageRes: Int) {
        bar_title_tv.text = title
        bar_back_iv.visibility = backVisibity
        bar_back_iv.setOnClickListener {
            finish()
        }

        bar_right_iv.visibility = rightVisibity
        if (rightVisibity == View.VISIBLE) {
            bar_right_iv.setImageResource(rightImageRes)
        }
    }

    /************************EventBus*****************************/
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: MessageEvent) {
        when (event.eventTag) {
            EventConstant.CancelHttpLoading -> {

            }
        }
    };

    public override fun onStart() {
        super.onStart()
        EventBus.getDefault().register(this)
    }

    public override fun onStop() {
        super.onStop()
        EventBus.getDefault().unregister(this)
    }


}